function [Images, Layers, Stamp, Vals, Keys] = readimg(FileName)

[~, name, ext] = fileparts(FileName);
% if strcmp(FileName(end - 2:end), 'aia')
if strcmp(ext, '.aia')
    
    %Open file and parse filename. Exception handling to close file in case
    %of error.
  
    try
        
        fid = fopen(FileName);
        Stamp = name;
        
        if length(Stamp) ~= 19
            disp('DateTime Stamp is not the correct format')
        end
        
        %Parse .aia file
        %First three are 'AIA'
        %add check to verify that...
		fread(fid,1);
        fread(fid,1);
        fread(fid,1);
		fread(fid,1);
        fread(fid,1); %unused .aia lines
        r = fread(fid, 1, 'uint16'); %rows
        c = fread(fid, 1, 'uint16'); %columns
        Layers = fread(fid, 1, 'uint16'); %layers
        %3D array holding images.
        Images = zeros(r, c, Layers);

        for ii = 1:Layers
            for jj = 1:r
                Images(jj, :, ii) = fread(fid, c, 'uint16');
            end
        end
 
        % read annotation at the end of file. This is in format ASCII 
        % character terminated by null character, then 64bit unsigned 
        % integer (double float).
        Keys= cell(0, 0);
        Vals = [];
        while ~feof(fid)
            curr = '';
            phrase = '';
            while ~strcmpi(char(0), curr) && ~feof(fid) %read until a null character occurs (char(0))
                curr = fread(fid, 1, '*char');
                phrase = strcat(phrase, curr);
            end
            if isempty(phrase)
                % to fix problem where after reading last number but before 
                % eof enters second while loop and puts empty strings on 
                % the end of everything.
            else
                cellphrase = cell(1 ,1); %create cell array then put phrase in it. Maybe there's a better way of dealing with these.
                cellphrase{1} = phrase;
                Keys=cat(2, Keys, cellphrase);
                newval=fread(fid, 1, 'double', 0); %then read off the number
                Vals=cat(2, Vals, newval);
            end
        end
        
        %close file
        fclose(fid);
        
        if length(Keys) ~= length(Vals)
            error('Keywords and vals of different lengths.')
        end
        
    catch err
        fclose(fid);
        disp(err.message)
    end
    
% elseif strcmp(FileName(end - 3:end), 'fits')
elseif strcmp(ext, '.fits')
    try
        data = fitsread(FileName);
        %Andor software number y-axis of arrays from the bottom. Matlab starts
        %from the top.
        Layers = size(data, 3);
        Images = flipdim(data, 1);
        Stamp = FileName;
%%%get metadata...this part works, but not sure that I want to print all of this info all of the time
%so want to fix that first.
%         info = fitsinfo(FileName);
%         Keys = info.PrimaryData.Keywords(:,1);
%         Vals = info.PrimaryData.Keywords(:,2);
%         IsNumeric = cellfun(@(x) isnumeric(x),Vals);
%         Keys = Keys(IsNumeric);
%         Vals = cell2mat(Vals(IsNumeric));
    catch err
        disp(err.message)
        Layers = 0;
        Images = NaN;
        Stamp = '';
    end
    Vals=[];
    Keys={};
end
